package com.onlyxiahui.framework.json.validator.impl;

import com.alibaba.fastjson.JSONObject;
import com.onlyxiahui.framework.json.validator.Validator;
import com.onlyxiahui.framework.json.validator.ValidatorError;
import com.onlyxiahui.framework.json.validator.ValidatorResult;
import com.onlyxiahui.framework.json.validator.util.RegexValidatorUtil;

/**
 * 
 * Date 2018-12-26 15:28:23<br>
 * Description url验证
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class JsonUrlValidator implements Validator<JSONObject, Object> {

	@Override
	public String getKey() {
		return "url";
	}

	@Override
	public ValidatorResult valid(JSONObject data, Object attribute, Object judge, String code, String message) {
		ValidatorResult vr = null;
		boolean mark = true;
		if (null != attribute) {
			String text = attribute.toString();
			if (!"".equals(text)) {
				mark = RegexValidatorUtil.isUrl(text);
			}
		}
		if (!mark) {
			vr = new ValidatorResult();
			vr.setErrorCode(code == null ? this.getCode() : code);
			vr.setMessage(message == null ? this.getMessage() : message);
		}
		return vr;
	}

	public String getMessage() {
		return "不是正确的url！";
	}

	public String getCode() {
		return ValidatorError.format_error;
	}
}
